package com.zhika.zishi.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.zhika.zishi.util.LogUtil;

/**
 * Created by shi-02 on 2015/5/4.
 */
public class DbHelper extends SQLiteOpenHelper {

    private static final String TAG = "DbHelper";

    //数据库的名字
    private static final String DATABASE_NAME = "zi_shi.db";

    //数据库的版本号
    private static final int DATABASE_VERSION = 1;

    //单例模式的dbHelper对象
    private static DbHelper dbHelper;

    /**
     * 单例模式创建DbHelper对象
     * @param context
     * @return
     */
    public static DbHelper getIntance(Context context){

        if (dbHelper==null){
            dbHelper = new DbHelper(context);
        }

        return dbHelper;
    }

    /**
     * 返回数据的版本号
     * @return
     */
    public static int getDatabaseVersion(){
        return DATABASE_VERSION;
    }

    public DbHelper(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }


    //表名
    public static final String TABLE_USER = "User";//用户的表

    //列名
    public static final String COLUMN_ID = "id"; //自增id列
    public static final String COLUMN_USER_ID = "userId"; //用户id
    public static final String COLUMN_USER_TOKEN = "userToken"; //用户的token
    public static final String COLUMN_USER_NICKNAME = "userNickname"; //用户的昵称
    public static final String COLUMN_IS_LOGIN = "isLogin"; //是否处于登录状态

    //创建用户的表
    private final String CREATE_USER =
            "CREATE TABLE IF NOT EXISTS " + TABLE_USER + "(" +
                    COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USER_ID + " BIGINT NOT NULL, " +
                    COLUMN_USER_NICKNAME + " VARCHAR(20) NOT NULL, " +
                    COLUMN_USER_TOKEN + " VARCHAR(20) NOT NULL, " +
                    COLUMN_IS_LOGIN + " INTEGER NOT NULL);";


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        LogUtil.e(TAG,"更新资势数据库");

        db.execSQL("DROP TABLE IF EXISTS" + DbHelper.TABLE_USER);

        //重新创建表
        onCreate(db);
    }
}
